<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\BaseController;
use App\Models\Manger\Mangerrule;
use App\Models\Steel\SteelAttrType;
use Illuminate\Support\Facades\Redis;

class RuleMenuController extends BaseController
{

    /**
     * 获取权限菜单
     */
    public function getmenulist()
    {
        $manger_id = $this->mangerinfo['id'];
        $utype = $this->mangerinfo['utype'];
        $cpid = $this->mangerinfo['cpid'];

        //缓存获取，无数据，数据库获取
        $menulist = Redis::get('manger_user_menulist_' . $manger_id);
        if ($menulist && is_array(json_decode($menulist, true))) {
            $menulist = json_decode($menulist, true);
        } else {
            $rule_info = Mangerrule::where(['mid' => $manger_id])->first();
            $menulist = json_decode($rule_info->ruleinfo, true);
            Redis::set('manger_user_menulist_' . $manger_id, $rule_info->ruleinfo);
        }


        foreach ($menulist as $key => $val) {
            if ($val['is_checked'] == 0) {
                unset($menulist[$key]);
            } else {
                if (isset($val['children'])) {
                    foreach ($val['children'] as $kkey => $vval) {
                        if ($vval['is_checked'] == 0) {
                            unset($menulist[$key]['children'][$kkey]);
                        } else {
                            if (isset($vval['children'])) {
                                foreach ($vval['children'] as $kkkey => $vvval) {
                                    if ($vvval['is_checked'] == 0) {
                                        unset($menulist[$key]['children'][$kkey]['children'][$kkkey]);
                                    } else {



                                        if (isset($vvval['children'])) {
                                            foreach ($vvval['children'] as $kkkkey => $vvvval) {
                                                if ($vvvval['is_checked'] == 0) {
                                                    unset($menulist[$key]['children'][$kkey]['children'][$kkkey]['children'][$kkkkey]);
                                                } else {
                                                    if (isset($vvvval['buttonlist'])) {
                                                        foreach ($vvvval['buttonlist'] as $bbbbkey => $bbbbvval) {
                                                            if ($bbbbvval['is_checked'] == 0) {
                                                                unset($menulist[$key]['children'][$kkey]['children'][$kkkey]['children'][$kkkkey]['buttonlist'][$bbbbkey]);
                                                            }
                                                        }
                                                        $menulist[$key]['children'][$kkey]['children'][$kkkey]['children'][$kkkkey]['buttonlist'] =
                                                            array_values($menulist[$key]['children'][$kkey]['children'][$kkkey]['children'][$kkkkey]['buttonlist']);
                                                    }
                                                }
                                            }
                                            $menulist[$key]['children'][$kkey]['children'][$kkkey]['children'] = array_values($menulist[$key]['children'][$kkey]['children'][$kkkey]['children']);
                                        }


                                        if (isset($vvval['buttonlist'])) {
                                            foreach ($vvval['buttonlist'] as $bbbkey => $bbbvval) {
                                                if ($bbbvval['is_checked'] == 0) {
                                                    unset($menulist[$key]['children'][$kkey]['children'][$kkkey]['buttonlist'][$bbbkey]);
                                                }
                                            }

                                            $menulist[$key]['children'][$kkey]['children'][$kkkey]['buttonlist'] =
                                                array_values($menulist[$key]['children'][$kkey]['children'][$kkkey]['buttonlist']);
                                        }
                                    }
                                }
                                $menulist[$key]['children'][$kkey]['children'] = array_values($menulist[$key]['children'][$kkey]['children']);
                            }

                            if (isset($vval['buttonlist'])) {
                                foreach ($vval['buttonlist'] as $bbkey => $bbvval) {
                                    if ($bbvval['is_checked'] == 0) {
                                        unset($menulist[$key]['children'][$kkey]['buttonlist'][$bbkey]);
                                    }
                                }

                                $menulist[$key]['children'][$kkey]['buttonlist'] =
                                    array_values($menulist[$key]['children'][$kkey]['buttonlist']);
                            }
                        }
                    }
                }

                if (isset($val['buttonlist'])) {
                    foreach ($val['buttonlist'] as $bkey => $bvval) {
                        if ($bvval['is_checked'] == 0) {
                            unset($menulist[$key]['buttonlist'][$bkey]);
                        }
                    }
                }

                $menulist[$key]['children'] = array_values($menulist[$key]['children']);
            }
        }
        $menulist = array_values($menulist);

        if ($utype == 1) { //动态配置菜单
            $list = SteelAttrType::where(['cpid' => $cpid, 'status' => 1])->get()->toArray();
            $attrtypemenu = [];
            foreach ($list as $attrtype) {
                $item = ['name' => 'steelssort-mate' . $attrtype['id'], 'path' => 'steelssort-mate' . $attrtype['id'], 'props' => ['id' => $attrtype['id']], 'component' => '/admin/steels/sort', 'meta' => ['title' => $attrtype['typename'], 'icon' => 'dashboard'], 'buttonlist' => [['name' => '新增'], ['name' => '导入'], ['name' => '导出'], ['name' => '模板下载']]];
                $attrtypemenu[] = $item;
            }
            foreach ($menulist as &$menuval) {
                if (isset($menuval['meta']['title']) && $menuval['meta']['title'] == '基础信息') {
                    //
                    foreach ($menuval['children'] as &$submenuval) {
                        if (isset($submenuval['meta']['title']) && $submenuval['meta']['title'] == '存货管理') {
                            $submenuval['children'] = array_merge($submenuval['children'], $attrtypemenu);
                        }
                    }
                }
            }
        }

        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $menulist]);
    }




    public function getmenulistnew(){
        $manger_id=$this->mangerinfo['id'];
        $utype=$this->mangerinfo['utype'];
        $cpid = $this->mangerinfo['cpid'];

        
        $menulist = Redis::get('manger_user_menulist_'.$manger_id.'_'.$utype);
            if ($menulist && is_array(json_decode($menulist, true))) {
                $menulist = json_decode($menulist, true);
            } else {
                $rule_info = Mangerrule::where(['mid' => $manger_id,'utype'=>$utype])->first();
                $menulist = json_decode($rule_info->ruleinfo, true);
                Redis::set('manger_user_menulist_'.$manger_id.'_'.$utype, $rule_info->ruleinfo);
            }

            foreach ($menulist as $key => $val) {
                if ($val['is_checked'] == 0) {
                    unset($menulist[$key]);
                } else {
                    if(isset($val['children'])){
                        foreach ($val['children'] as $kkey => $vval) {
                            if ($vval['is_checked'] == 0) {
                                unset($menulist[$key]['children'][$kkey]);
                            }else{
                                if(isset($vval['children'])){
                                    foreach ($vval['children'] as $kkkey => $vvval) {
                                        if ($vvval['is_checked'] == 0) {
                                            unset($menulist[$key]['children'][$kkey]['children'][$kkkey]);
                                        }else{
                                            if(isset($vvval['meta']['buttonList'])){
                                                foreach ($vvval['meta']['buttonList'] as $bbbkey => $bbbvval) {
                                                    if ($bbbvval['is_checked'] == 0) {
                                                        unset($menulist[$key]['children'][$kkey]['children'][$kkkey]['meta']['buttonList'][$bbbkey]);
                                                    }
                                                }
                                                $menulist[$key]['children'][$kkey]['children'][$kkkey]['meta']['buttonList']=array_values($menulist[$key]['children'][$kkey]['children'][$kkkey]['meta']['buttonList']);
                                            }
                                        }
                                        
                                    }
                                    $menulist[$key]['children'][$kkey]['children']=array_values($menulist[$key]['children'][$kkey]['children']);
                                }
    
                                if(isset($vval['meta']['buttonList'])){
                                    foreach ($vval['meta']['buttonList'] as $bbkey => $bbvval) {
                                        if ($bbvval['is_checked'] == 0) {
                                            unset($menulist[$key]['children'][$kkey]['meta']['buttonList'][$bbkey]);
                                        }
                                    }
                                    $menulist[$key]['children'][$kkey]['meta']['buttonList']=array_values($menulist[$key]['children'][$kkey]['meta']['buttonList']);
                                }
                            }
                            
                        }
                        $menulist[$key]['children']=array_values($menulist[$key]['children']);
                    }
    
                    if(isset($val['meta']['buttonList'])){
                        foreach ($val['meta']['buttonList'] as $bkey => $bvval) {
                            if ($bvval['is_checked'] == 0) {
                                unset($menulist[$key]['meta']['buttonList'][$bkey]);
                            }
                        }
                        $menulist[$key]['meta']['buttonList']=array_values($menulist[$key]['meta']['buttonList']);
                    }
                    
                }
            }
        

        if ($utype == 1) { //动态配置菜单
            $list = SteelAttrType::where(['cpid' => $cpid, 'status' => 1])->get()->toArray();
            
            $attrtypemenu = [];
            foreach ($list as $attrtype) {
                $item = ['name' => 'steelssort-mate' . $attrtype['id'],
                 'path' => 'steelssort-mate' . $attrtype['id'], 
                 'props' => ['id' => $attrtype['id']], 'component' => '/admin/steels/sort', 
                 'meta' => ['title' => $attrtype['typename'], 'icon' => 'dashboard'],
                  'buttonlist' => [['name' => '新增'], ['name' => '导入'], ['name' => '导出'], ['name' => '模板下载']]];
                $attrtypemenu[] = $item;
            }
            foreach ($menulist as &$menuval) {
                if (isset($menuval['meta']['title']) && $menuval['meta']['title'] == '基础信息') {
                    //
                    
                    foreach ($menuval['children'] as &$submenuval) {
                        if (isset($submenuval['meta']['title']) && $submenuval['meta']['title'] == '存货管理') {
                            // print_r($submenuval);exit;
                            $submenuval['children'] = array_merge($submenuval['children'], $attrtypemenu);
                        }
                    }
                }
            }
        }

        $menulist=array_values($menulist);


        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $menulist]);

    }
}
